<!--suppress ALL -->
<include file="./Template/header.html" />

<link rel="stylesheet" type="text/css" href="/Template/css/order.css">
<script type="text/javascript" src="/Template/js/amazeui.js"></script>
<script type="text/javascript">


	$(function() {

		//初始化页面配置
		initPage("订单确认");

		var app = new Vue({
			el: '#container',
			data: {
				number: 1,
				singlePrice: "{$product.price}",
				myIcoin: "{$customer.point}",
			},
			methods: {
				calculate: function(num) {
					if((num+app.number)>0) {
						app.number += num;
					}
				},
				measure: function(id) {
					var data = {};
					data.number = app.number;
					data.receiveName = $("input[name=receiveName]").val();
					data.receiveAddress = $("input[name=receiveAddress]").val();
					data.receiveContact = $("input[name=receiveContact]").val();
                    data.doctorName = $("input[name=doctorName]").val();
                    data.hospitalName = $("input[name=hospitalName]").val();
					data.note = $("input[name=note]").val();
					data.needInvoice = $("select[name=needInvoice]").val();
					data.invoiceName = $("input[name=invoiceName]").val();
                    data.invoiceNumber = $("input[name=invoiceNumber]").val();
					data.productId = "{$product.product_id}";
					data.deliveryType = $("select[name=deliveryType]").val();
					data.addressId = $("select[name=addressId]").val();

					localStorage.setItem("orderData", JSON.stringify(data));

					window.location.href = "/wechat/order/measure/id/"+id;
				},
				createOrder: function() {

					var number = app.number;
					var receiveName = $("input[name=receiveName]").val();
					var receiveAddress = $("input[name=receiveAddress]").val();
					var receiveContact = $("input[name=receiveContact]").val();
					var needDoctor = "{$category.need_doctor}";
					var doctorName = $("input[name=doctorName]").val();
                    var hospitalName = $("input[name=hospitalName]").val();
					var note = $("input[name=note]").val();
					var needInvoice = $("select[name=needInvoice]").val();
					var invoiceName = $("input[name=invoiceName]").val();
                    var invoiceNumber = $("input[name=invoiceNumber]").val();
					var productId = "{$product.product_id}";
					var needMeasure = $("input[name=needMeasure]").val();
					var deliveryType = $("select[name=deliveryType]").val();

					if(deliveryType == "上门自取") {
						receiveAddress = $("select[name=addressId]").find("option:selected").text()
								+ " " + $("select[name=addressId]").find("option:selected").attr("address");
					}

					if(isNullOrEmpty(receiveName)) {
						alert("请填写收货人");
						return false;
					}

					if(isNullOrEmpty(receiveAddress)) {
						alert("请填写收货人地址");
						return false;
					}

					if(isNullOrEmpty(receiveContact)) {
						alert("请填写收货人电话");
						return false;
					}

					if(!checkPhone(receiveContact)) {
						alert("手机号位数不对");
						return false;
					}

                    if(needDoctor == 1) {
                        if (isNullOrEmpty(doctorName)) {
                            alert("请填写医生姓名");
                            return false;
                        }

                        if (isNullOrEmpty(hospitalName)) {
                            alert("请填写医院名称");
                            return false;
                        }
                    }

					if(needInvoice == 1) {
						if(isNullOrEmpty(invoiceName)) {
							alert("请填写发票抬头");
							return false;
						}
					}

                    if(needInvoice == 1) {
                        if(isNullOrEmpty(invoiceNumber)) {
                            alert("请填写纳税人识别号");
                            return false;
                        }
                    }

					var data = {};

					data.number = number;
					data.receiveName = receiveName;
					data.receiveAddress = receiveAddress;
					data.receiveContact = receiveContact;
                    data.doctorName = doctorName;
                    data.hospitalName = hospitalName;
					data.productId = productId;
					data.invoiceName = invoiceName;
                    data.invoiceNumber = invoiceNumber;
					data.needInvoice = needInvoice;
					data.needMeasure = needMeasure;
					data.note = note;
					data.deliveryType = deliveryType;

					if(needMeasure == "1") {
						var measureData = $("input[name=measureJson]").val();
						if(isNullOrEmpty(measureData)) {
							alert("请先填写测量");
							return false;
						}
						data.measureData = measureData;
					}

					$.ajax({
						url:"/wechat/order/createOrder",
						type:"post",
						data:data,
						dataType:"JSON",
						beforeSend:function(){
							$("#loadingToast").show();
						},
						complete:function(){
							$('#loadingToast').hide();
						},
						success:function(data){
							if(data.code != 200) {
								alert(data.msg);
								return false;
							}
							window.localStorage.setItem("orderData", null);
							window.localStorage.setItem("measureData", null);
							window.location.href = "/wechat/userCenter/payInfo/id/"+data.order_id;
						},
					})

				}
			}
		})

		$(".item select").change(function() {
			var value = $(this).find("option:selected").text();
			$(this).prev().val(value);
		});

		$("select[name=needInvoice]").change(function() {
			if($(this).val() == 1) {
				$(".item.invoice").show();
			} else {
				$(".item.invoice").hide();
			}
		});

		$("select[name=deliveryType]").change(function() {
			if($(this).val() == "上门自取") {
				$(".item.choose-shop").show();
				$(".shop-address").html("具体地址："+$("select[name=addressId]").find("option:selected").attr("address"));
				$(".item.receiveAddress").hide();
			} else {
				$(".item.choose-shop").hide();
				$(".item.receiveAddress").show();
			}
		});

		$("select[name=addressId]").change(function() {
			var value = $(this).find("option:selected").text();
			$(this).prev().val(value);
			$(".shop-address").html("具体地址："+$("select[name=addressId]").find("option:selected").attr("address"));
		});

		$("input#addressId").val($("select[name=addressId]").find("option:selected").text());
		$(".shop-address").html("具体地址："+$("select[name=addressId]").find("option:selected").attr("address"));

		var leftData = window.localStorage.getItem("orderData");
		if(!isNullOrEmpty(leftData)) {
			var orderData = JSON.parse(leftData);

			if(!isNullOrEmpty(orderData)) {

				if(orderData.productId == "{$product.product_id}") {

					app.number = orderData.number;
					$("input[name=receiveName]").val(orderData.receiveName);
					$("input[name=receiveAddress]").val(orderData.receiveAddress);
					$("input[name=receiveContact]").val(orderData.receiveContact);
                    $("input[name=doctorName]").val(orderData.doctorName);
                    $("input[name=hospitalName]").val(orderData.hospitalName);
					$("input[name=note]").val(orderData.note);
					$("input[name=invoiceName]").val(orderData.invoiceName);
                    $("input[name=invoiceNumber]").val(orderData.invoiceNumber);
					$("select[name=needInvoice]").val(orderData.needInvoice);
					$("select[name=deliveryType]").val(orderData.deliveryType);
					$("input#transport").val(orderData.deliveryType);
					$("select[name=addressId]").val(orderData.addressId);
					$("input#addressId").val($("select[name=addressId]")
							.find("option:selected").text());
					$("p.shop-address").html("具体地址："+$("select[name=addressId]")
									.find("option:selected").attr("address"));

					var value = $("select[name=needInvoice]").find("option:selected").text();
					$("select[name=needInvoice]").prev().val(value);

					if($("select[name=needInvoice]").val() == 1) {
						$(".item.invoice").show();
					} else {
						$(".item.invoice").hide();
					}

					if($("select[name=deliveryType]").val() == "上门自取") {
						$(".receiveAddress").hide();
						$(".choose-shop").show();
					} else {
						$(".receiveAddress").show();
						$(".choose-shop").hide();
					}

				} else {
					window.localStorage.setItem("orderData", null);
					window.localStorage.setItem("measureData", null);
				}

			}

		}

		var measureData = window.localStorage.getItem("measureData");

		if(!isNullOrEmpty(measureData)) {
			measureData = JSON.parse(measureData);
			if(!isNullOrEmpty(measureData)) {

				if(measureData.productId == "{$product.product_id}") {
					$("input[name=measureJson]").val(JSON.stringify(measureData.data));
                    var i = 0;
                    var dataStr = "";
                    for(var j in measureData.data){
                        if( dataStr != "")
                            dataStr += ", ";
                        dataStr += measureData.data[j]["value"];
                    }
					$("input[name=measureData]").val(dataStr);
				}

			}

		}

		<if condition="$confirmInfo eq 1">
			alert("对不起，该疾病需要填写医生资料");
			window.location.href = "/wechat/userCenter/myInfo";
		</if>

	})
</script>

<div class="container" id="container">
	<div class="product-detail">
		<img class="product-img" src="{$product.image_url}" alt="product">
		<div class="detail-info">
			<p class="product-price">￥{$product.price}</p>
			<p class="product-name">{$product.product_name}</p>
			<p class="number">×{{number}}</p>
			<p class="buy-time">订购日期：{:date("Y-m-d")}</p>
		</div>
	</div>
	<div class="number-and-transport">
		<div class="item">
			<span>购买数量</span>
			<span class="number-change">
                    <img class="icon-minus" v-on:click="calculate(-1)" src="/Template/images/minus.png" alt="">
                    <span class="sum-number">{{number}}</span>
                    <img class="icon-plus" v-on:click="calculate(1)" src="/Template/images/plus.png" alt="">
                </span>
		</div>
		<div class="item">
			<span>配送方式</span>
			<input id="transport" value="快递 免邮" type="text" readonly placeholder="请选择配送方式">
			<select name="deliveryType">
				<option value="快递 免邮">快递 免邮</option>
				<option value="上门自取">上门自取</option>
			</select>
			<img class="icon-right gender" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item choose-shop" style="display:none;">
			<span>自取地址</span>
			<input id="addressId" value="" type="text" readonly placeholder="请选择自取门店">
			<select name="addressId">
				{:getAddressSelectOptionHtml(0,0)}
			</select>
			<img class="icon-right gender" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item choose-shop" style="color:#4293e0;font-size:0.857rem;display: none;line-height: 2rem;height: auto;">
			<p class="shop-address"></p>
		</div>
	</div>
	<div class="address-info">
		<div class="item">
			<span>收货人姓名</span>
			<input type="text" name="receiveName" placeholder="请填写收货人姓名">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item">
			<span>收货人电话</span>
			<input type="text" name="receiveContact" placeholder="请填写收货人电话">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item receiveAddress">
			<span>收货地址</span>
			<input type="text" name="receiveAddress" placeholder="请填写收货地址">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
	</div>
	<div class="doctor-info">
		<if condition="$category['need_doctor'] eq 1">
			<div class="item">
				<span>医生</span>
				<input type="text" value="{$customer.doctor_name}" name="doctorName" placeholder="请填写医生姓名">
				<img class="icon-right" src="/Template/images/icon-right.png" alt="">
			</div>
			<div class="item">
				<span>医院</span>
				<input type="text" value="{$customer.hospital_name}" name="hospitalName" placeholder="请填写医院名称">
				<img class="icon-right" src="/Template/images/icon-right.png" alt="">
			</div>
		</if>
	</div>
	<div class="product-need">
		<input type="hidden" name="needMeasure" value="{$product.need_measure}" />
		<if condition="$product['need_measure'] eq 1">
			<div class="item" v-on:click="measure({$product.product_id})">
				<span>此产品需要测量</span>
				<input type="text" name="measureData" readonly placeholder="请编辑测量信息">
				<input type="hidden" name="measureJson" />
				<img class="icon-right" src="/Template/images/icon-right.png" alt="">
			</div>
		</if>
		<div class="item">
			<span>留言备注</span>
			<input type="text" name="note" placeholder="请编辑备注信息">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item">
			<span>是否开发票</span>
			<input type="text" value="不开发票" name="needInvoice" readonly placeholder="">
			<select name="needInvoice" class="invoice-choose">
				<option value="-1">不开发票</option>
				<option value="1">公司发票</option>
				<option value="2">个人发票</option>
			</select>
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item invoice" style="display: none;">
			<span>发票抬头</span>
			<input name="invoiceName" type="text" placeholder="请填写发票抬头">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
		<div class="item invoice" style="display: none;">
			<span>纳税人识别号</span>
			<input name="invoiceNumber" type="text" placeholder="请填纳税人识别号">
			<img class="icon-right" src="/Template/images/icon-right.png" alt="">
		</div>
	</div>
	<div class="order-operation">
		<if condition="$product['category_id'] eq 2">
		<p>共{{number}}件商品  减免I币<span>{{myIcoin}}</span>后合计：<span>￥{{number*singlePrice-myIcoin}}</span> </p>
			<else/>
		<p>共{{number}}件商品  合计：<span>￥{{number*singlePrice}}</span> </p>
		</if>
		<button class="submit-order" v-on:click="createOrder">提交订单</button>
	</div>
</div>
<include file="./Template/footer.html" />